package com.smg.community.controller;

import cn.org.rapid_framework.page.Page;
import com.smg.community.controller.thread.AutoJoinActivityThread;
import com.smg.community.controller.thread.AutoReplyThread;
import com.smg.community.model.SmgGroup;
import com.smg.community.model.SmgGroupCategory;
import com.smg.community.model.SmgMember;
import com.smg.community.model.SmgMemberBehavior;
import com.smg.community.model.SmgTopic;
import com.smg.community.model.SmgTopicActivityMember;
import com.smg.community.model.SmgTopicActivitySetting;
import com.smg.community.model.SmgTopicReply;
import com.smg.community.service.SmgGroupCategoryManager;
import com.smg.community.service.SmgGroupManager;
import com.smg.community.service.SmgKeywordManager;
import com.smg.community.service.SmgMemberBehaviorManager;
import com.smg.community.service.SmgMemberManager;
import com.smg.community.service.SmgTopicActivityMemberManager;
import com.smg.community.service.SmgTopicActivitySettingManager;
import com.smg.community.service.SmgTopicManager;
import com.smg.community.service.SmgTopicReplyManager;
import com.smg.community.setting.Cache;
import com.smg.community.setting.Constant;
import com.smg.community.setting.PointSetting;
import com.smg.community.util.ProjectUtil;
import com.smg.community.util.StringUtil;
import com.smg.community.vo.query.SmgTopicQuery;
import com.smg.community.vo.query.SmgTopicReplyQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javacommon.base.BaseRestSpringController;
import javacommon.util.HtmlToText;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/topic"})
public class SmgTopicController extends BaseRestSpringController<SmgTopic, Integer>
{
  private static final long serialVersionUID = 1L;

  @RequestMapping({"/createreply"})
  @Transactional
  public String createReply(SmgTopicReply reply, ModelMap model, HttpServletRequest request)
  {
    SmgMember loginMember;
    try
    {
      loginMember = getLoginMember(request);
      if (loginMember == null)
        return toErrorPage(model, "您未登录或者登录已超时");

      int topicId = reply.getFkTopicId().intValue();
      SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
      reply.setCateName(topic.getCateName());
      reply.setCityName(topic.getCityName());
      reply.setCityNo(topic.getCityNo());
      reply.setFkCateId(topic.getFkCateId());
      reply.setFkGroupId(topic.getFkGroupId());
      reply.setGroupName(topic.getGroupName());
      List keywordList = this.smgKeywordManager.getKeywordList();
      Map resultMap = StringUtil.filterKeyword(reply.getReplyContBeforFilter(), keywordList);
      String replyContAfterFilter = (String)resultMap.get("cont");
      String containKeywords = (String)resultMap.get("containKeywords");
      if (containKeywords.length() == 0) {
        reply.setIsReplyContainKeyword(Integer.valueOf(0));
        reply.setReplyContainKeywords("");
      } else {
        reply.setIsReplyContainKeyword(Integer.valueOf(1));
        reply.setReplyContainKeywords(containKeywords);
      }
      reply.setReplyContAfterFilter(replyContAfterFilter);
      reply.setIsValid(Integer.valueOf(1));
      reply.setReplyDate(new Date());
      reply.setReplyMemberHead(loginMember.getHeadImage());
      reply.setReplyMemberId(loginMember.getMemberId());
      reply.setReplyMemberName(loginMember.getNickName());
      this.smgTopicReplyManager.save(reply);
      this.smgTopicManager.updateLastReply(reply);
      this.smgMemberManager.addPoint(PointSetting.CREATE_REPLY_TYPE, PointSetting.CREATE_REPLY, "回复了话题  《 " + topic.getTopicTitleAfterFilter() + "》", loginMember, request);
      SmgMemberBehavior smgMemberBehavior = new SmgMemberBehavior();
      smgMemberBehavior.setCont("回复了话题  " + topic.getTopicTitleAfterFilter());
      smgMemberBehavior.setHeadImage(loginMember.getHeadImage());
      smgMemberBehavior.setMemberId(loginMember.getMemberId());
      smgMemberBehavior.setNickName(loginMember.getNickName());
      smgMemberBehavior.setContPreview(HtmlToText.getPlainText(topic.getTopicContAfterFilter()));
      smgMemberBehavior.setCreateMemberId(loginMember.getMemberId().intValue());
      smgMemberBehavior.setCreateMemberName(loginMember.getNickName());
      smgMemberBehavior.setFkGroupId(topic.getFkGroupId().intValue());
      smgMemberBehavior.setGroupName(topic.getGroupName());
      smgMemberBehavior.setSourcefrom("pc");
      smgMemberBehavior.setType(Constant.BEHAVIOR_REPLY_TOPIC);
      smgMemberBehavior.setUrl(ProjectUtil.getTopicDetailUrl(request, topicId));
      smgMemberBehavior.setImage(ProjectUtil.getImageFromTopicCont(topic.getTopicContAfterFilter()));
      smgMemberBehavior.setRelatedId(reply.getReplyId().intValue());
      smgMemberBehavior.setTitle(topic.getTopicTitleAfterFilter());
      this.smgMemberBehaviorManager.save(smgMemberBehavior);
    } catch (Exception e) {
      e.printStackTrace();
      return toErrorPage(model, e);
    }
    int pageNumber = this.smgTopicManager.getPageNumberAfterReply(reply.getFkTopicId().intValue());
    return "redirect:/topic/detail/d/" + reply.getFkTopicId() + "-" + pageNumber + "#" + reply.getReplyId();
  }

  @RequestMapping({"/createtopic"})
  @Transactional
  public String createTopic(SmgTopic smgTopic, ModelMap model, HttpServletRequest request)
  {
    SmgMember loginMember = getLoginMember(request);
    if (loginMember == null)
      return toErrorPage(model, "您未登录或者登录已超时");

    int groupId = smgTopic.getFkGroupId().intValue();
    ProjectUtil.filterTopic(smgTopic);
    try
    {
      if (smgTopic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
        smgTopic.setTitleTypeName("活动");
        smgTopic.setFkTitleTypeId(Integer.valueOf(99));
      } else if (smgTopic.getTopicType().intValue() == Constant.TOPIC_TYPE_VOTE)
      {
        smgTopic.setTitleTypeName("投票");
        smgTopic.setFkTitleTypeId(Integer.valueOf(98));
      } else {
        String titleTypeName = ProjectUtil.getTopicTitleName(smgTopic.getFkTitleTypeId().intValue());
        smgTopic.setTitleTypeName(titleTypeName);
      }

      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(Integer.valueOf(groupId));
      String groupName = group.getGroupName();
      smgTopic.setGroupName(groupName);
      int cateId = group.getFkCateId().intValue();
      smgTopic.setFkCateId(Integer.valueOf(cateId));
      int cityNo = group.getCityNo();
      smgTopic.setCityNo(Integer.valueOf(cityNo));
      String cityName = group.getCityName();
      smgTopic.setCityName(cityName);
      String cateName = ((SmgGroupCategory)this.smgGroupCategoryManager.getById(Integer.valueOf(cateId))).getCateName();
      smgTopic.setCateName(cateName);
      smgTopic.setCreateMemberHead(loginMember.getHeadImage());
      smgTopic.setClickCount(Integer.valueOf(0));
      smgTopic.setCreateDate(new Date());
      smgTopic.setCreateMemberName(loginMember.getNickName());
      smgTopic.setFkCreateMemberId(loginMember.getMemberId());
      smgTopic.setIsEssence(Integer.valueOf(0));
      smgTopic.setIsRecommend(Integer.valueOf(0));
      smgTopic.setReplyCount(Integer.valueOf(0));
      smgTopic.setIsValid(Integer.valueOf(1));
      List keywordList = this.smgKeywordManager.getKeywordList();

      String topicTitleAfterFilter = (String)StringUtil.filterKeyword(smgTopic.getTopicTitleBeforeFilter(), keywordList).get("cont");
      Map resultMap = StringUtil.filterKeyword(smgTopic.getTopicContBeforeFilter(), keywordList);
      String topicContAfterFilter = (String)resultMap.get("cont");
      String containKeywords = (String)resultMap.get("containKeywords");
      if (containKeywords.length() == 0) {
        smgTopic.setIsTopicContainKeyWord(Integer.valueOf(0));
        smgTopic.setTopicContainKeyWords("");
      } else {
        smgTopic.setIsTopicContainKeyWord(Integer.valueOf(1));
        smgTopic.setTopicContainKeyWords(containKeywords);
      }
      smgTopic.setTopicContAfterFilter(topicContAfterFilter);
      smgTopic.setTopicTitleAfterFilter(topicTitleAfterFilter);
      if ((smgTopic.getPicview() == null) || (smgTopic.getPicview().equals("")))
        smgTopic.setPicview(ProjectUtil.getImageFromTopicCont(smgTopic.getTopicContAfterFilter()));

      this.smgTopicManager.save(smgTopic);
      this.smgMemberManager.addTopicCount(smgTopic.getFkCreateMemberId().intValue());
      this.smgMemberManager.addPoint(PointSetting.CREATE_TOPIC_TYPE, PointSetting.CREATE_TOPIC, "发布了一个新话题  《 " + smgTopic.getTopicTitleAfterFilter() + "》", loginMember, request);
      SmgMemberBehavior smgMemberBehavior = new SmgMemberBehavior();
      smgMemberBehavior.setCont("发表了话题  " + smgTopic.getTopicTitleAfterFilter());
      smgMemberBehavior.setHeadImage(loginMember.getHeadImage());
      smgMemberBehavior.setMemberId(loginMember.getMemberId());
      smgMemberBehavior.setNickName(loginMember.getNickName());
      smgMemberBehavior.setContPreview(HtmlToText.getPlainText(smgTopic.getTopicContAfterFilter()));
      smgMemberBehavior.setCreateMemberId(loginMember.getMemberId().intValue());
      smgMemberBehavior.setCreateMemberName(loginMember.getNickName());
      smgMemberBehavior.setFkGroupId(smgTopic.getFkGroupId().intValue());
      smgMemberBehavior.setGroupName(smgTopic.getGroupName());
      smgMemberBehavior.setSourcefrom("pc");
      smgMemberBehavior.setType(Constant.BEHAVIOR_CREATE_TOPIC);
      smgMemberBehavior.setUrl(ProjectUtil.getTopicDetailUrl(request, smgTopic.getTopicId().intValue()));
      smgMemberBehavior.setImage(ProjectUtil.getImageFromTopicCont(smgTopic.getTopicContAfterFilter()));
      smgMemberBehavior.setRelatedId(smgTopic.getTopicId().intValue());
      smgMemberBehavior.setTitle(smgTopic.getTopicTitleAfterFilter());

      if (smgTopic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
        smgTopic.setTitleTypeName("活动");
        int topicId = smgTopic.getTopicId().intValue();
        SmgTopicActivitySetting activity = new SmgTopicActivitySetting();
        activity.setActivityType(Integer.valueOf(Integer.parseInt(request.getParameter("activityType"))));
        activity.setEndDate(smgTopic.getEndDate());
        activity.setStartDate(smgTopic.getStartDate());
        activity.setFkTopicId(Integer.valueOf(topicId));
        activity.setPoint(Integer.valueOf(Integer.parseInt(request.getParameter("point"))));
        smgMemberBehavior.setCont("发布了一个新活动  《 " + smgTopic.getTopicTitleAfterFilter() + "》");
        smgMemberBehavior.setImage(smgTopic.getPicview());
        smgMemberBehavior.setType(Constant.BEHAVIOR_CREATE_ACTIVITY);
        this.smgTopicActivitySettingManager.save(activity);
      } else if (smgTopic.getTopicType().intValue() == Constant.TOPIC_TYPE_VOTE)
      {
        smgTopic.setTitleTypeName("投票");
        smgMemberBehavior.setType(Constant.BEHAVIOR_CREATE_VOTE);
        smgMemberBehavior.setCont("发起了一项投票  《 " + smgTopic.getTopicTitleAfterFilter() + "》");
      }

      Thread autoReplyThread = new AutoReplyThread(this.smgMemberBehaviorManager, this.smgTopicManager, this.smgTopicReplyManager, this.smgMemberManager, 3, smgTopic.getTopicId().intValue(), loginMember);
      autoReplyThread.start();
      this.smgMemberBehaviorManager.save(smgMemberBehavior);
    }
    catch (Exception e) {
      e.printStackTrace();
      return toErrorPage(model, "出错了..", e);
    }
    String from = request.getParameter("from");
    return "redirect:" + from;
  }

  @RequestMapping({"/intocreatetopic/{groupId}"})
  public String intoCreateTopic(ModelMap model, HttpServletRequest request, @PathVariable int groupId)
  {
    try
    {
      setCommunityCommonModel(request, model);
      SmgMember loginMember = getLoginMember(request);
      if (loginMember == null)
        return toErrorPage(model, "您未登录或者登录已超时");

      model.addAttribute("topicTitleTypeList", Cache.TopicTitleTypeList);
      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(Integer.valueOf(groupId));
      model.addAttribute("group", group);
      setHeadTabIndex(model, Constant.FORUM_TAB);
    } catch (Exception e) {
      e.printStackTrace();
      return toErrorPage(model, e);
    }
    return "/topic/create";
  }

  @RequestMapping({"/intocreateactivity/{groupId}"})
  public String intoCreateActivity(ModelMap model, HttpServletRequest request, @PathVariable int groupId)
  {
    try
    {
      setCommunityCommonModel(request, model);
      SmgMember loginMember = getLoginMember(request);
      if (loginMember == null)
        return toErrorPage(model, "您未登录或者登录已超时");

      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(Integer.valueOf(groupId));
      model.addAttribute("group", group);
      setHeadTabIndex(model, Constant.ACTIVITY_TAB);
    } catch (Exception e) {
      e.printStackTrace();
      return toErrorPage(model, e);
    }
    return "/topic/createactivity";
  }

  @RequestMapping({"/detail/{topicId}"})
  public String topicDetailDefault(ModelMap model, HttpServletRequest request, @PathVariable int topicId)
  {
    return topicDetail(model, request, topicId, 1);
  }

  @RequestMapping({"/joinactivity/{topicId}"})
  @Transactional
  public String joinActivity(ModelMap model, HttpServletRequest request, @PathVariable int topicId, SmgTopicActivityMember activityMember)
  {
    SmgMember loginMember = getLoginMember(request);
    if (loginMember == null) {
      return toErrorPage(model, "您未登录不能参与活动");
    }

    SmgTopicActivitySetting activitySetting = this.smgTopicActivitySettingManager.getByTopicId(topicId);
    if ((activitySetting.getActivityType().intValue() != 1) && 
      (activitySetting.getActivityType().intValue() == 3)) {
      int point = activitySetting.getPoint().intValue();
      int memberPoint = loginMember.getAvilablePoint();
      if (memberPoint < point)
        return toErrorPage(model, "您没有足够的正能量参加这个活动!");

      this.smgMemberManager.subPoint(loginMember.getMemberId(), point);
    }
    activityMember.setFkTopicId(Integer.valueOf(topicId));
    activityMember.setFkMemberId(loginMember.getMemberId());
    activityMember.setJoinDate(new Date());
    activityMember.setHeadImage(loginMember.getHeadImage());
    activityMember.setNickName(loginMember.getNickName());
    this.smgTopicActivityMemberManager.save(activityMember);
    SmgTopic smgTopic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    this.smgMemberManager.addPoint(PointSetting.JOIN_ACTIVITY_TYPE, PointSetting.JOIN_ACTIVITY, "参加活动  " + smgTopic.getTopicTitleAfterFilter(), loginMember, request);
    SmgMemberBehavior smgMemberBehavior = new SmgMemberBehavior();
    smgMemberBehavior.setCont("参加了活动  " + smgTopic.getTopicTitleAfterFilter());
    smgMemberBehavior.setHeadImage(loginMember.getHeadImage());
    smgMemberBehavior.setMemberId(loginMember.getMemberId());
    smgMemberBehavior.setNickName(loginMember.getNickName());
    smgMemberBehavior.setContPreview(HtmlToText.getPlainText(smgTopic.getTopicContAfterFilter()));
    smgMemberBehavior.setCreateMemberId(loginMember.getMemberId().intValue());
    smgMemberBehavior.setCreateMemberName(loginMember.getNickName());
    smgMemberBehavior.setFkGroupId(smgTopic.getFkGroupId().intValue());
    smgMemberBehavior.setGroupName(smgTopic.getGroupName());
    smgMemberBehavior.setSourcefrom("pc");
    smgMemberBehavior.setType(Constant.BEHAVIOR_JOIN_ACTIVITY);
    smgMemberBehavior.setUrl(ProjectUtil.getTopicDetailUrl(request, smgTopic.getTopicId().intValue()));
    smgMemberBehavior.setImage(smgTopic.getPicview());
    smgMemberBehavior.setRelatedId(smgTopic.getTopicId().intValue());
    smgMemberBehavior.setTitle(smgTopic.getTopicTitleAfterFilter());
    this.smgMemberBehaviorManager.save(smgMemberBehavior);
    return topicDetail(model, request, topicId, 1);
  }

  @RequestMapping({"/detail/d/{topicId}-{pageNumber}"})
  public String topicDetail(ModelMap model, HttpServletRequest request, @PathVariable int topicId, @PathVariable int pageNumber)
  {
    SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    try {
      String message;
      if (topic == null) {
        message = "你查看的话题不存在";
        return toErrorPage(model, message);
      }
      if (topic.getIsValid().intValue() == 0) {
        message = "";
        if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_NORMAL) {
          message = "你查看的话题不存在或者已经被删除";
          this.smgMemberBehaviorManager.deleteByRelatedIdAndType(topic.getTopicId().intValue(), Constant.BEHAVIOR_CREATE_TOPIC);
        }
        if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
          message = "你查看的活动不存在或者已经被删除";
          this.smgMemberBehaviorManager.deleteByRelatedIdAndType(topic.getTopicId().intValue(), Constant.BEHAVIOR_CREATE_ACTIVITY);
        }
        return toErrorPage(model, message);
      }
      ProjectUtil.filterTopic(topic);
      setHeadTabIndex(model, Constant.FORUM_TAB);
      setCommunityCommonModel(request, model);
      model.addAttribute("topic", topic);
      int cateId = topic.getFkCateId().intValue();
      SmgGroupCategory category = (SmgGroupCategory)this.smgGroupCategoryManager.getById(Integer.valueOf(cateId));
      model.addAttribute("category", category);
      int groupId = topic.getFkGroupId().intValue();
      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(Integer.valueOf(groupId));
      model.addAttribute("group", group);
      model.addAttribute("isGroupMaster", Boolean.valueOf(false));

      int cityNo = topic.getCityNo().intValue();
      List smgTopicList = this.smgTopicManager.getFourHotTopicList(cityNo);
      model.addAttribute("smgTopicList", smgTopicList);

      List activityList = this.smgTopicManager.geteActivityListOnline(cityNo, 4);
      model.addAttribute("activityList", activityList);
      this.smgTopicManager.updateClickCount(topicId);
      SmgTopicReplyQuery query = new SmgTopicReplyQuery();
      query.setPageNumber(pageNumber);
      query.setFkTopicId(Integer.valueOf(topicId));
      query.setPageSize(30);
      setStartAndLimit(query, pageNumber, 30);
      Page replyPage = this.smgTopicReplyManager.findPage(query);
      model.addAttribute("replyPage", replyPage);
      model.addAttribute("pageNumber", Integer.valueOf(pageNumber));
      SmgMember loginMember = getLoginMember(request);
      model.addAttribute("isSelf", Boolean.valueOf(false));
      if (loginMember != null) {
        if (loginMember.getMemberId().intValue() == topic.getFkCreateMemberId().intValue())
          model.addAttribute("isSelf", Boolean.valueOf(true));

        if ((group != null) && 
          (loginMember.getMemberId().intValue() == group.getFkMemberId().intValue())) {
          model.addAttribute("isGroupMaster", Boolean.valueOf(true));
        }

      }

      if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
        setHeadTabIndex(model, Constant.ACTIVITY_TAB);
        SmgTopicActivitySetting activity = this.smgTopicActivitySettingManager.getByTopicId(topicId);
        model.addAttribute("activity", activity);
        if (loginMember == null) {
          model.addAttribute("hasJoin", Boolean.valueOf(false));
        } else {
          boolean hasJoin = this.smgTopicActivityMemberManager.getHasJoinActivity(loginMember.getMemberId().intValue(), topicId);
          model.addAttribute("hasJoin", Boolean.valueOf(hasJoin));
        }

        List activityMemberList = this.smgTopicActivityMemberManager.getMemberListByTopicId(topicId);
        model.addAttribute("activityMemberList", activityMemberList); 
      }
      topic.getTopicType().intValue();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      return toErrorPage(model, e);
    }

     String description = topic.getTopicContAfterFilter();
    description = HtmlToText.getPlainText(description);
    if (description.length() > 100)
      description = description.substring(0, 100);

    model.addAttribute("description", description);
    return "/topic/detail";
  }

  @RequestMapping({"/setTopicTopType"})
  @ResponseBody
  public Map<String, String> setTopicTopType(HttpServletRequest request, HttpServletResponse response)
  {
    List topicList;
    String topType = request.getParameter("topType");
    SmgMember member = getLoginMember(request);
    String topicIdStr = request.getParameter("topicId");
    int topicId = Integer.parseInt(topicIdStr);
    SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    if (topic == null) {
      return writeErrorMessage("话题不存在!");
    }

    if (member == null)
      return writeErrorMessage("您未登录!");

    SmgTopicQuery query = new SmgTopicQuery();
    query.setTopType("topType = " + topType);
    query.setLimit(10);
    if (topType.equals(Constant.GROUP_TOP_TYPE)) {
      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(topic.getFkGroupId());
      if (group == null)
        return writeErrorMessage("群组不存在");

      if ((member.getIsStaff() == 1) || (group.getFkMemberId().intValue() == member.getMemberId().intValue())) {
        if (topType.equals(Constant.GROUP_TOP_TYPE))
          query.setFkGroupId(group.getGroupId());

        query.setIsValid(Integer.valueOf(1));
        topicList = this.smgTopicManager.getByQuery(query);
        if (topicList.size() <= 0)
        return writeErrorMessage("已有一条置顶贴,操作失败!");
      }

      return writeErrorMessage("您没有操作的权限");
    }
    if (topType.equals(Constant.CATEGORY_TOP_TYPE)) {
      query.setFkCateId(topic.getFkCateId());
      topicList = this.smgTopicManager.getByQuery(query);
      if (topicList.size() <= 1)
      return writeErrorMessage("已有二条分类置顶贴,操作失败!");
    }
    if (topType.equals(Constant.FULL_SIZT_TOP_TYPE)) {
      query.setCityNo(topic.getCityNo());
      topicList = this.smgTopicManager.getByQuery(query);
      if (topicList.size() <= 2)
      return writeErrorMessage("已有三条全站置顶贴,操作失败!");
    }


    topic.setTopType(Integer.parseInt(topType));
    topic.setLastOperateLog(member.getNickName() + " 于  " + DateFormatUtils.format(new Date(), "yyyy-mm-dd hh:MM:ss") + " 设置置顶");
    this.smgTopicManager.updateTopType(topic);
    return writeSuccessMessage(); }

  @RequestMapping({"/cancleTopicTopType"})
  @ResponseBody
  public Map<String, String> cancleTopicTopType(HttpServletRequest request, HttpServletResponse response) {
    String topType = request.getParameter("topType");
    SmgMember member = getLoginMember(request);
    String topicIdStr = request.getParameter("topicId");
    int topicId = Integer.parseInt(topicIdStr);
    SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    if (topic == null)
      return writeErrorMessage("话题不存在!");

    if (member == null)
      return writeErrorMessage("您未登录!");

    SmgTopicQuery query = new SmgTopicQuery();
    query.setTopType(topType);
    if (topType.equals(Constant.GROUP_TOP_TYPE)) {
      SmgGroup group = (SmgGroup)this.smgGroupManager.getById(topic.getFkGroupId());
      if (group == null)
        return writeErrorMessage("群组不存在");

      if ((member.getIsStaff() == 1) || (group.getFkMemberId().intValue() == member.getMemberId().intValue()))

      return writeErrorMessage("您没有操作的权限");
    }
    if (topType.equals(Constant.CATEGORY_TOP_TYPE)) {
      if (member.getIsStaff() == 1)

      return writeErrorMessage("您没有操作的权限");
    }
    if (topType.equals(Constant.FULL_SIZT_TOP_TYPE)) {
      if (member.getIsStaff() == 1)

      return writeErrorMessage("您没有操作的权限");
    }


    topic.setTopType(0);
    topic.setLastOperateLog("");
    this.smgTopicManager.updateTopType(topic);
    return writeSuccessMessage(); }

  @RequestMapping({"/deleteTopic"})
  @ResponseBody
  public Map<String, String> deleteTopic(HttpServletRequest request, HttpServletResponse response) {
    SmgMember member = getLoginMember(request);
    String topicIdStr = request.getParameter("topicId");
    int topicId = Integer.parseInt(topicIdStr);
    SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    if (topic == null)
      return writeErrorMessage("话题不存在!");

    if (member == null)
      return writeErrorMessage("您未登录!");

    if ((member.getIsStaff() == 1) || (topic.getFkCreateMemberId().intValue() == member.getMemberId().intValue())) {
      this.smgTopicManager.removeById(Integer.valueOf(topicId));
      if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_NORMAL)
        this.smgMemberBehaviorManager.deleteByRelatedIdAndType(topic.getTopicId().intValue(), Constant.BEHAVIOR_CREATE_TOPIC);

      if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY)
        this.smgMemberBehaviorManager.deleteByRelatedIdAndType(topic.getTopicId().intValue(), Constant.BEHAVIOR_CREATE_ACTIVITY);

      return writeSuccessMessage();
    }
    return writeErrorMessage("您没有操作的权限");
  }

  @RequestMapping({"/deleteReply"})
  public void deleteReply(HttpServletRequest request, HttpServletResponse response)
  {
    SmgMember member = getLoginMember(request);
    String replyIdStr = request.getParameter("replyId");
    int replyId = Integer.parseInt(replyIdStr);
    SmgTopicReply reply = (SmgTopicReply)this.smgTopicReplyManager.getById(Integer.valueOf(replyId));
    if (reply == null) {
      writeErrorMessage("回帖不存在!");
      return;
    }
    if ((member.getIsStaff() == 1) || (reply.getReplyMemberId().intValue() == member.getMemberId().intValue())) {
      this.smgTopicReplyManager.removeById(Integer.valueOf(replyId));
      this.smgMemberBehaviorManager.deleteByRelatedIdAndType(reply.getReplyId().intValue(), Constant.BEHAVIOR_REPLY_TOPIC);
      writeSuccessMessage();
    } else {
      writeErrorMessage("您没有操作的权限");
      return;
    }
  }

  @RequestMapping({"/edit/{topicId}"})
  public String edit(HttpServletRequest request, HttpServletResponse response, @PathVariable int topicId, ModelMap model)
  {
    setHeadTabIndex(model, Constant.FORUM_TAB);
    setCommunityCommonModel(request, model);
    SmgMember member = getLoginMember(request);
    SmgTopic topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(topicId));
    if (member == null)
    {
      return toErrorPage(model, "您未登录");
    }
    if ((member.getIsStaff() != 1) && (topic.getFkCreateMemberId().intValue() != member.getMemberId().intValue()))
    {
      return toErrorPage(model, "您没有操作的权限");
    }
    model.addAttribute("topic", topic);
    int groupId = topic.getFkGroupId().intValue();
    SmgGroup group = (SmgGroup)this.smgGroupManager.getById(Integer.valueOf(groupId));
    if (group == null)
      return toErrorPage(model, "群组不存在!");

    model.addAttribute("group", group);
    if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_NORMAL)
      return "topic/edit";
    if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
      SmgTopicActivitySetting activitySetting = (SmgTopicActivitySetting)this.smgTopicActivitySettingManager.getById(topic.getTopicId());
      model.addAttribute("activitySetting", activitySetting);
      return "topic/editactivity";
    }
    return "";
  }

  @RequestMapping({"/edit/action/{topicId}"})
  public String editAction(HttpServletRequest request, HttpServletResponse response, @PathVariable int topicId, ModelMap model, SmgTopic topic)
  {
    SmgMember member = getLoginMember(request);
    if ((member.getIsStaff() != 1) && (topic.getFkCreateMemberId().intValue() != member.getMemberId().intValue()))
    {
      return toErrorPage(model, "您没有操作的权限");
    }
    topic.setLastOperateLog(member.getNickName() + "于" + DateFormatUtils.format(new Date(), "yyyy-mm-dd hh:MM:dd") + " 进行了编辑");
    List keywordList = this.smgKeywordManager.getKeywordList();
    String topicTitleAfterFilter = (String)StringUtil.filterKeyword(topic.getTopicTitleBeforeFilter(), keywordList).get("cont");
    Map resultMap = StringUtil.filterKeyword(topic.getTopicContBeforeFilter(), keywordList);
    String topicContAfterFilter = (String)resultMap.get("cont");
    String containKeywords = (String)resultMap.get("containKeywords");
    if (containKeywords.length() == 0) {
      topic.setIsTopicContainKeyWord(Integer.valueOf(0));
      topic.setTopicContainKeyWords("");
    } else {
      topic.setIsTopicContainKeyWord(Integer.valueOf(1));
      topic.setTopicContainKeyWords(containKeywords);
    }
    topic.setTopicContAfterFilter(topicContAfterFilter);
    topic.setTopicTitleAfterFilter(topicTitleAfterFilter);
    if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_NORMAL)
      topic.setPicview(ProjectUtil.getImageFromTopicCont(topic.getTopicContAfterFilter()));

    this.smgTopicManager.updateTopicTitleAndCont(topic);
    if (topic.getTopicType().intValue() == Constant.TOPIC_TYPE_ACTIVITY) {
      SmgTopicActivitySetting activitySetting = new SmgTopicActivitySetting();
      activitySetting.setFkTopicId(topic.getTopicId());
      activitySetting.setStartDate(topic.getStartDate());
      activitySetting.setEndDate(topic.getEndDate());
      activitySetting.setPoint(Integer.valueOf(Integer.parseInt(request.getParameter("point"))));
      activitySetting.setActivityType(Integer.valueOf(Integer.parseInt(request.getParameter("activityType"))));
      this.smgTopicActivitySettingManager.update(activitySetting);
    }
    return topicDetail(model, request, topicId, 1); }

  @RequestMapping({"/exitactivity"})
  @ResponseBody
  public Map<String, String> exitactivity(HttpServletRequest request) {
    SmgMember loginMember = getLoginMember(request);
    Map map = new HashMap();
    if (loginMember == null) {
      map.put("state", "-1");
      map.put("desc", "您未登录或者登录已超时");
      return map;
    }
    String topicIdStr = request.getParameter("topicId");
    int topicId = Integer.parseInt(topicIdStr);
    this.smgTopicActivityMemberManager.deleteByMemberIdAndTopicId(loginMember.getMemberId(), topicId);
    map.put("state", "1");
    return map; }

  @RequestMapping({"/autoAddReplyAll"})
  @ResponseBody
  public Map<String, String> autoAddReplyAll() { SmgTopicQuery query;
    try { query = new SmgTopicQuery();
      query.setIsValid(Integer.valueOf(1));
      query.setLimit(500);
      Page page = this.smgTopicManager.findPage(query);
      for (Iterator localIterator = page.iterator(); localIterator.hasNext(); ) { SmgTopic topic = (SmgTopic)localIterator.next();
        SmgMember loginMember = this.smgMemberManager.getRandomMember();
        Random random = new Random();
        int count = random.nextInt(10);
        Thread autoReplyThread = new AutoReplyThread(this.smgMemberBehaviorManager, this.smgTopicManager, this.smgTopicReplyManager, this.smgMemberManager, count, topic.getTopicId().intValue(), loginMember);
        autoReplyThread.start();
      }
      return writeSuccessMessage("自动回复线程启动"); } catch (Exception e) {
    }
    return writeErrorMessage("启动失败,请检查参数"); }

  @RequestMapping({"/autoAddReply"})
  @ResponseBody
  public Map<String, String> autoAddReply(@RequestParam String topicId, @RequestParam String count) {
    SmgTopic topic;
    try {
      topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(Integer.parseInt(topicId)));
      if (topic == null)
        return writeErrorMessage("topicId=" + topicId + "的话题不存在");

      SmgMember loginMember = this.smgMemberManager.getRandomMember();
      Thread autoReplyThread = new AutoReplyThread(this.smgMemberBehaviorManager, this.smgTopicManager, this.smgTopicReplyManager, this.smgMemberManager, Integer.parseInt(count), Integer.parseInt(topicId), loginMember);
      autoReplyThread.start();
      return writeSuccessMessage("自动回复线程启动"); } catch (Exception e) {
    }
    return writeErrorMessage("启动失败,请检查参数"); }

  @RequestMapping({"/autoJoinActivity"})
  @ResponseBody
  public Map<String, String> autoJoinActivity(@RequestParam String topicId, @RequestParam String count) {
    SmgTopic topic;
    try {
      topic = (SmgTopic)this.smgTopicManager.getById(Integer.valueOf(Integer.parseInt(topicId)));
      if (topic == null)
        return writeErrorMessage("topicId=" + topicId + "的话题不存在");

      if (topic.getTopicType().intValue() != Constant.TOPIC_TYPE_ACTIVITY)
        return writeErrorMessage("topicId=" + topicId + "的话题不是活动类型");

      SmgMember loginMember = this.smgMemberManager.getRandomMember();
      Thread autoJoinActivity = new AutoJoinActivityThread(this.smgMemberBehaviorManager, this.smgTopicManager, this.smgTopicActivityMemberManager, this.smgMemberManager, Integer.parseInt(count), Integer.parseInt(topicId), loginMember);
      autoJoinActivity.start();
      return writeSuccessMessage("自动回复线程启动"); } catch (Exception e) {
    }
    return writeErrorMessage("启动失败,请检查参数");
  }
}